// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
import storage from "../../Script/SdkTools/storage";
const { ccclass, property } = cc._decorator;

// 游戏启动监听场景值
console.log("游戏启动监听场景值 window['tt']:", typeof window['tt']);
if (typeof window['tt'] == 'object') {
    console.log('tt init success');
    let tt = window['tt'];
    tt.onShow((res: any) => {
        console.log('启动参数：', res.query);
        console.log('来源信息：', res.refererInfo);
        console.log('场景值：', res.scene);
        console.log('启动场景字段：', res.launch_from, ', ', res.location);

        cc.sys.localStorage.setItem('isLaunchFromSidebar', 'false');

        if (
            (res.launch_from == 'homepage' && res.location == 'sidebar_card') ||
            res.scene == '021036'
        ) {
            cc.sys.localStorage.setItem('isLaunchFromSidebar', 'true');
        }
        if (
            (res.launch_from == 'game_room' && res.location == 'game_list') ||
            res.scene == '021036'
        ) {
            cc.sys.localStorage.setItem('isLaunchFromSidebar', 'true');
        }
        console.log('isLaunchFromSidebar:' + cc.sys.localStorage.getItem('isLaunchFromSidebar'));
    });
}

@ccclass
export default class Giftbar extends cc.Component {
    @property(cc.Node)
    panelNode: cc.Node = null;
    @property(cc.Label)
    giftLabel: cc.Label = null;
    @property(cc.Node)
    giftNode: cc.Node = null;
    tt: any = null;
    start() {
        cc.game.addPersistRootNode(this.node);
        //非字节平台隐藏sliderbar
        this.initTT();
        this.initGiftbar();
    }

    initGiftbar() {
        this.giftNode.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.giftNode.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.giftNode.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
    }

    onTouchStart(event) {}

    onTouchMove(event: cc.Event.EventTouch) {
        const size = this.node.getContentSize();
        const giftSize = this.giftNode.getContentSize();
        const to = new cc.Vec3(
            event.getLocationX() - size.width / 2,
            event.getLocationY() - size.height / 2
        );
        if (to.x < -size.width / 2 + giftSize.width / 2) {
            to.x = -size.width / 2 + giftSize.width / 2;
        }
        if (to.y < -size.height / 2 + giftSize.height / 2) {
            to.y = -size.height / 2 + giftSize.height / 2;
        }

        if (to.x > size.width / 2 - giftSize.width / 2) {
            to.x = size.width / 2 - giftSize.width / 2;
        }
        if (to.y > size.height / 2 - giftSize.height / 2) {
            to.y = size.height / 2 - giftSize.height / 2;
        }

        this.giftNode.position = to;
    }

    onTouchEnd(event) {}

    initTT() {
        if (typeof window['tt'] == 'undefined') {
            this.node.active = false;
            return;
        }
        const _this = this;
        const _giftLabel = this.giftLabel;
        this.tt = window['tt'];
        // 检查是否存在侧边栏
        this.tt.checkScene({
            scene: 'sidebar',
            success: (res: any) => {
                console.log('check scene success: ', res.isExist);
                //成功回调逻辑
                if (!res.isExist) {
                    // 是否展示领奖入口
                    _this.node.active = false;
                } else {
                    _this.node.active = true;

                    // 判断是否是从侧边栏进入
                    if (cc.sys.localStorage.getItem('isLaunchFromSidebar') == 'true') {
                        if (cc.sys.localStorage.getItem('LaunchFromSidebarGift') == 'true') {
                            _giftLabel.string = '奖励已领取';
                        } else {
                            _giftLabel.string = '领取奖励';
                        }
                    } else {
                        _giftLabel.string = '侧边栏领奖';
                    }
                }
            },
            fail: (res) => {
                //失败回调逻辑
                console.log('check scene fail:', res);
            },
        });
    }

    clickSilderBar() {
        console.log('clickSliderBar');
        if (this.panelNode.active == false) {
            this.panelNode.active = true;
            cc.director.pause();
        } else {
            this.panelNode.active = false;
            cc.director.resume();
        }
    }

    backSilderBar() {
        console.log('backSliderBar');
        let btnText = this.giftLabel.string;
        if (btnText == '侧边栏领奖') {
            console.log('45789');
            // 返回侧边栏
            this.tt.navigateToScene({
                scene: 'sidebar',
                success: (res: any) => {
                    // 跳转成功回调逻辑
                    console.log('navigate to scene success', res);
                },
                fail: (res) => {
                    // 跳转失败回调逻辑
                    console.log('navigate to scene fail: ', res);
                },
            });
        } else if (btnText == '领取奖励') {
            if (cc.sys.localStorage.getItem('LaunchFromSidebarGift') == 'true') {
                this.giftLabel.string = '奖励已领取';
                return;
            }

            // 领取奖励
            console.log('领取侧边栏奖励');
            storage.Instance.setEnergy(storage.Instance.nowEnery+2);
            //cc.sys.localStorage.setItem("no_ad_time", new Date().getTime() + 10 * 60 * 1000); // 十分钟免广告
            cc.sys.localStorage.setItem('LaunchFromSidebarGift', 'true');
            this.giftLabel.string = '奖励已领取';
            console.log('领取侧边栏奖励成功');
        } else if (btnText == '奖励已领取') {
            console.log('奖励已领取');
        }
    }
}
